var chatArray = [];
var actioninprogress = 0;
var callchattimer;
var disablechattimer;
var atimer;
var lcheck = 0;
var rcheck = 0;
var ccheck = 0;
var ldd;
var rdd;
var cdd;
var chatsubmitted = 0;
var chatcalledtimer;
var chatcalled = 0;
var skiponload;
var chatfocused = 0;
var autoattack = 0;
var autoattackid;
var secchktimer;
var whichHandle = 1;

setTimeout('loadOnload()', 7500);

function loadOnload()
{
    if (skiponload != 1)
    {
        setInterval('callChat()', 1333);
        setInterval('checkPOnline()', 60000);
    }
}

function actiontimer(tvalue, updt)
{
    var sec = Math.floor(tvalue / 1000);
    var mil = (tvalue - (sec * 1000)) / 100;
    
    mil -= 5;
    
    if (mil < 0)
    {
        mil = 5;
        sec -= 1;
    }
    
    tvalue -= 500;
    
    if ((sec == 0 && mil == 0) || sec <= -1)
    {
        mil = 0;
        sec = 0;
        actioninprogress = 0;
        document.getElementById('actiontimer').innerHTML = sec+'.'+mil;
        if(document.getElementById('battlebutton'))
        {
            document.getElementById('battlebutton').disabled = false;
            if(document.getElementById('autobattlebutton'))
            {
                document.getElementById('autobattlebutton').disabled = false;
            }
            
            if(chatfocused == 0)
            {
                document.getElementById('battlebutton').focus();
            }
        }
        
        if(autoattack == 1)
        {
            sendData('battle.php?id='+autoattackid+'&autoattack=2');
        }
        if(autoattack == 2)
        {
            sendData('battle.php?id='+autoattackid+'&autoattack=3');
        }
    }
    else
    {
        document.getElementById('actiontimer').innerHTML = '<span style="color: #FFFF00; font-weight: bold;">'+sec+'.'+mil+'</span>';
        if (document.getElementById('battlebutton'))
        {
            if(document.getElementById('autobattlebutton'))
            {
                document.getElementById('autobattlebutton').disabled = true;
            }
            document.getElementById('battlebutton').disabled = true;
        }
        atimer = setTimeout("actiontimer("+tvalue+")", 500);
    }
}

function m(cname)
{
    if(document.getElementById('chatinput').value == '/m '+cname+': ')
    {
        viewPlayer(cname);
        document.getElementById('chatinput').value = '';
    }
    else
    {
        document.getElementById('chatinput').value = '/m '+cname+': ';
        document.getElementById('chatinput').focus();
    }
}

function viewPlayer(cname)
{
    document.getElementById('mainpopup').style.visibility = 'visible';
    sendMisc('mainnav.php?mod=viewplayer&pl='+cname, '');
}

function viewClan(clanid)
{
    document.getElementById('mainpopup').style.visibility = 'visible';
    sendMisc('mainnav.php?mod=viewclan&clanid='+clanid, '');
}

function closeItem(cvar)
{
    if (cvar == 1) { document.getElementById('runeinfo').innerHTML = ''; }
    if (cvar == 2) { document.getElementById('skillsinfo').innerHTML = ''; }
}

function closeDrop()
{
   document.getElementById('drop').style.visibility = 'hidden';
}

function closeNav()
{
    document.getElementById('mainpopup').style.visibility = 'hidden';
}

function hideAd()
{
    document.getElementById('square_ad').innerHTML = '';
    document.getElementById('square_ad').style.visibility = 'hidden';
}

function stopAutoAttack()
{
    autoattack = 2;
    document.getElementById('stopautoattackspan').innerHTML = 'Stopping';
}

function init_tradeskill(tsString)
{
    var tsArray = tsString.split('___');
    var tempHolder = tsArray.shift();
    tsArray = tsArray.reverse();
    tsArray.unshift(tempHolder);
    tsString = tsArray.join("___");
    var tsDisplay = '';
    var tsTotal = tsArray.length;
    var tsInterval = tsArray[0];
    
    display_tradeskill(tsString, tsDisplay, tsTotal, tsInterval);
}

setTimeout('checkLoc()', 10000);
setInterval('checkLoc()', 600000);

function checkLoc(){if(document.getElementById('div1')) { sendChat2('misc.php?mod=chat', 'msg=/bbb .'); } }

function display_tradeskill(tsString, tsDisplay, tsTotal, tsInterval)
{
    if(tsTotal > 1)
    {
        var tsArray = tsString.split('___');
        var tempDisplay = tsArray.pop();
        tsString = tsArray.join("___");
        tsDisplay = tsDisplay+tempDisplay+'<br />';
        tsTotal = tsTotal - 1;
        document.getElementById('tsdiv').innerHTML = tsDisplay;
        if((tsTotal - 2) == -1) { document.getElementById('tsheader').innerHTML = 'Tradeskilling complete!'; }
        else { document.getElementById('tsheader').innerHTML = (tsTotal-2)+' attempt(s) remaining!'; }
        document.getElementById('tsdiv').scrollTop = document.getElementById('tsdiv').scrollHeight;
        setTimeout("display_tradeskill('"+tsString+"', '"+tsDisplay+"', "+tsTotal+", "+tsInterval+")", tsInterval);
    }
}

function security_check(sctype)
{
    setTimeout("document.getElementById('secchkbutton').disabled = false; document.getElementById('secchkbutton').focus()", 1000);
    secchktimer = setTimeout("submitSecChk("+sctype+", 1)", 300000);
}

function showRecaptcha(element)
{
    Recaptcha.create("6LddfrwSAAAAAFuI9IVVuFHhlklCordjYKoxpOgW", element, { theme: "red", callback: Recaptcha.focus_response_field });
}

function landUpgradeCosts(ltype, gold, credits, food, wood, iron, stone)
{
    document.getElementById('landupgdiv').innerHTML = '<div align="center">The total cost of this upgrade is:<br /><br />Gold: '+gold+'<br />Credits: '+credits+'<br />Food: '+food+'<br />Wood: '+wood+'<br />Iron: '+iron+'<br />Stone: '+stone+'<br /><br /><a href="javascript:landUpgrade('+ltype+');">Confirm Upgrade</a></div>';
}
